﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DMA.SGDB.DAL
{
    public class ClientePJDAL
    {
        public void Insert(CLIENTES_PJ cliente)
        {
            try
            {
                using (var db = new DMA_SGDBEntities())
                {
                    db.AddToCLIENTES_PJ(cliente);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(CLIENTES_PJ cliente)
        {
            try
            {
                EntityKey key;
                object originalItem;

                using (var db = new DMA_SGDBEntities())
                {
                    key = db.CreateEntityKey("CLIENTES_PJ", cliente);

                    if (db.TryGetObjectByKey(key, out originalItem))
                    {
                        db.ApplyCurrentValues(key.EntitySetName, cliente);
                    }

                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete(int idCliente)
        {
            try
            {
                using (var db = new DMA_SGDBEntities())
                {
                    var recordToBeExcluded = db.CLIENTES_PJ.Single(r => r.COD_CLI == idCliente);
                    db.CLIENTES_PJ.DeleteObject(recordToBeExcluded);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<CLIENTES_PJ> GetAll()
        {
            try
            {
                using (var db = new DMA_SGDBEntities())
                {
                    return db.CLIENTES_PJ.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public CLIENTES_PJ LoadById(int idCLIENTES_PJ)
        {
            try
            {
                using (var db = new DMA_SGDBEntities())
                {
                    return db.CLIENTES_PJ.Single(r => r.COD_CLI == idCLIENTES_PJ);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<CLIENTES_PJ> GetDuplicateRecord(CLIENTES_PJ clientePJ)
        {
            try
            {
                using (var db = new DMA_SGDBEntities())
                {
                    if (string.IsNullOrEmpty(clientePJ.CNPJ_CLI))
                        return db.CLIENTES_PJ.Where(r => r.CNPJ_CLI == clientePJ.CNPJ_CLI).ToList();
                    else
                        return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<CLIENTES_PJ> GetDuplicateDescription(CLIENTES_PJ CLIENTES_PJ)
        {
            try
            {
                using (var db = new DMA_SGDBEntities())
                {
                    //return db.CLIENTES_PJ.Where(r => r.IdCLIENTES_PJ != CLIENTES_PJ.IdCLIENTES_PJ && r.Description.ToUpper().Equals(CLIENTES_PJ.Description.ToUpper())).ToList();
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
